package com.example.rk.controller;

import com.example.rk.common.MyPageInfo;
import com.example.rk.common.SessionParameter;
import com.example.rk.entity.*;
import com.example.rk.service.ChoiceService;
import com.example.rk.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/choice")
public class ChoiceController {
    @Autowired
    @Resource
    private ChoiceService service;
    @RequestMapping("/list")
    public ModelAndView main(HttpServletRequest request, ModelAndView mv) {
        MyPageInfo myPageInfo = service.findBySearch(request);
        mv.addObject("searchtitle", request.getParameter("title"));
        mv.addObject("pageInfo", myPageInfo);
        mv.setViewName("/gly/choicelist");
        return mv;
    }
    @RequestMapping("/doAdd")
    @ResponseBody
    public String doAdd(Choice choice) {
        return service.doAdd(choice);
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id) {
        service.deleteById(id);
        return "redirect:/choice/list";
    }
    @RequestMapping("/list1/{id}")
    public ModelAndView main1(HttpServletRequest request, ModelAndView mv,@PathVariable("id") int id) {
        MyPageInfo myPageInfo = service.findBySearch1(request,id);
        mv.addObject("searchtitle", request.getParameter("title"));
        mv.addObject("pageInfo", myPageInfo);
        mv.setViewName("/gly/choicelist1");
        return mv;
    }
    @RequestMapping("/open/{id}")
    public ModelAndView openEdit(@PathVariable("id") int id, ModelAndView mv,HttpServletRequest request) {
        Reply reply = service.findById(id);
        mv.addObject("reply", reply);
        mv.setViewName("/gly/choice");
        return mv;
    }
    @RequestMapping("/delete1/{id}")
    public String delete1(@PathVariable("id") int id) {
        service.deleteById1(id);
//        int cid =service.fingcid(id);
        return "redirect:/choice/list";}
    @RequestMapping("/list2")
    public ModelAndView main2(HttpServletRequest request, ModelAndView mv)
    {
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        String type=admin.getType();
        MyPageInfo myPageInfo = service.findBySearch2(request,type,admin.getId());
        mv.addObject("searchtitle", request.getParameter("title"));
        mv.addObject("pageInfo", myPageInfo);
        if(admin.getType().equals("学生")){
        mv.setViewName("/xs/choicelist");}
        else if(admin.getType().equals("公司")){
            mv.setViewName("/gs/choicelist");
        }
        return mv;
    }

    @RequestMapping("/doEdit/{id}")
    public ModelAndView doEdit(@PathVariable("id") int id, ModelAndView mv,HttpServletRequest request) {
        Choice choice= service.findById1(id);
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        mv.addObject("choice", choice);
        if(admin.getType().equals("学生")){
        mv.setViewName("/xs/replyedit");
        }else if(admin.getType().equals("公司")){
            mv.setViewName("/gs/replyedit");
        }
        return mv;
    }
    @RequestMapping("/Edit")
    @ResponseBody
    public String Edit(Reply reply,HttpServletRequest request){
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        reply.setName(admin.getName());
        reply.setSid(admin.getId());
        return service.doEdit(reply);
    }
    @RequestMapping("/listzz")
    public ModelAndView main3(HttpServletRequest request,ModelAndView mv)
    {
        MyPageInfo myPageInfo = service.findBySearch3(request);
        mv.addObject("searchname", request.getParameter("name"));
        mv.addObject("searchdizhi", request.getParameter("dizhi"));
        mv.addObject("pageInfo", myPageInfo);
        mv.setViewName("/gly/changelist");
        return mv;
    }
    @RequestMapping("/listjy")
    public ModelAndView main4(HttpServletRequest request,ModelAndView mv)
    {
        MyPageInfo myPageInfo = service.findBySearch4(request);
        mv.addObject("searchname", request.getParameter("name"));
        mv.addObject("pageInfo", myPageInfo);
        mv.setViewName("/gly/jylist");
        return mv;
    }
    @RequestMapping("/Edit1/{id}")
    public String Edit1(@PathVariable("id") int id, HttpServletRequest request){
        int cid=service.find(id);
//        String name=service.findname(id);
//        service.setname(name,cid);
        service.findByIdty(cid);
        service.deleteById2(id);
        return "redirect:/choice/listzz";
    }

    @RequestMapping("/tupian/{id}")
    @ResponseBody
    public ModelAndView tupian(@PathVariable("id") int id, ModelAndView mv,HttpServletRequest request){
        Changest changest= service.findByIdtp(id);
        mv.addObject("changest", changest);
            mv.setViewName("/gly/tupian");
        return mv;
    }
    @RequestMapping("/deletejl/{id}")
    public String delete2(@PathVariable("id") int id) {
        service.deleteById2(id);
        return "redirect:/choice/listzz";}
    @RequestMapping("/jy")
    @ResponseBody
    public String jy(Contract contract,HttpServletRequest request) {
        Admin admin = (Admin) request.getSession().getAttribute(SessionParameter.ADMIN_INFO);
        contract.setName(admin.getName());
        contract.setCid(admin.getId());
        return service.jy(contract);
    }
    @RequestMapping("/jydelete/{id}")
    public String delete3(@PathVariable("id") int id) {
        service.jydeleteById(id);
        return "redirect:/choice/listjy";}
    @RequestMapping("/jyEdit/{id}")
    public String Edit2(@PathVariable("id") int id, HttpServletRequest request){
        int cid=service.jyfind(id);
        service.jyfindById(cid);
        service.jydeleteById(id);
        return "redirect:/choice/listjy";
    }
}

